<?php

namespace Apps\Frontend\Controllers;


use Apps\Common\Libs\PicUrl;
use Apps\Common\Models\Activity;
use Apps\Common\Models\ActivityBeautyParlor;
use Apps\Common\Models\BeautyParlor;
use Apps\Common\Models\BeautyParlorComment;
use Apps\Common\Models\BeautyParlorPhotos;
use Apps\Common\Models\BeautyParlorService;
use Apps\Common\Models\Question;
use Apps\Common\Models\QuestionAnswer;
use Phalcon\Mvc\Controller;
use Apps\Common\Models\CosBeautyCanon;
use Apps\Common\Models\ActivityComment;
use Apps\Common\Models\BeautyParlorTagInfo;
use Apps\Common\Models\QuestionAnswerComments;
use Apps\Common\Models\BeautyParlorInteractive;
use Apps\Common\Models\UserBase;

class ShareController extends Controller
{
    private $appName;
    public function initialize()
    {
        $this->appName = $this->di->get('moduleConfig')->get('application')->id;
        $this->assets->collection('header');
        $this->assets->collection('footer');

        $this->assets->addCss('/assets/frontend/css/core.css', false);

        $this->assets->collection('header')
            ->addJs('/assets/frontend/js/share/fontsize.js', false);

        $this->assets->collection('footer');
            //->addJs('/libs/jquery/jquery-2.1.4.min.js', false);
            //->addJs('/libs/zepto/zepto-1.1.6.min.js', false)
            //->addJs('/libs/zepto/detect.js', false);
    }

    /**
     * 问答
     */
    public function questionAction()
    {
        $id = $this->request->getQuery('id');
        if(empty($id)) {
            echo '问题不存在！';
            $this->view->disable();
            return;
        }
        $limit = 20;
        $page = 1;
        $this->tag->setTitle( $this->appName . ' - 问答详情');
        $question = Question::query()
            ->columns(['u.user_id',
                'u.user_nickname',
                'u.user_cover',
                'Apps\Common\Models\Question.question_id as question_id',
                'Apps\Common\Models\Question.question_content as question_content',
                'Apps\Common\Models\Question.question_pics as question_pics',
                'Apps\Common\Models\Question.question_browsers as answer_num',
                'Apps\Common\Models\Question.question_follows as follow_num',
                '0 user_is_follow',
                'Apps\Common\Models\Question.question_addtime as addtime'])
            ->leftJoin('Apps\Common\Models\UserBase',
                'Apps\Common\Models\Question.user_id=u.user_id','u')
            ->where('question_state>0 and question_id=:id:')
            ->bind(['id'=>$id])
            ->execute()->getFirst();

        if($question) {
            $question->question_pics = PicUrl::Question(json_decode($question->question_pics), $this->di);
            $question->addtime = \Apps\Common\Libs\DateTime::FriendlyDate($question->addtime, 'mohu');
            $question->user_cover = PicUrl::UserCover($question->user_cover, $this->di);
            if (unserialize(base64_decode($question->question_content))){
                $question ->question_content = unserialize(base64_decode($question->question_content));
            }
        } else {
            echo '问题不存在！';
            $this->view->disable();
            return;
        }
        $this->view->setVar('question', $question);

        $answers = QuestionAnswer::listByQuestionId($id, $limit, 0, 0)->toArray();
        $answers = $answers ? $answers : [];
        $answersData = [];
        $dataBest = null;
        foreach($answers as $arr) {
            if (unserialize(base64_decode($arr['answer_content']))){
                $arr['answer_content'] = unserialize(base64_decode($arr['answer_content']));
            }
            $arr['user_cover'] = PicUrl::UserCover($arr['user_cover'], $this->di);
            $arr['answer_pics'] = PicUrl::Question(json_decode($arr['answer_pics']), $this->di);
            $arr['addtime'] = \Apps\Common\Libs\DateTime::FriendlyDate($arr['addtime'], 'mohu');
            // 处理最佳答案
            $best = $arr['answer_best'];
            if($best > 5 && $page == 1) {
                if($dataBest == null) {
                    $dataBest = $arr;
                    continue;
                } elseif($dataBest['answer_best'] < $best) {
                    $dataBest = $arr;
                    continue;
                }
            }
            $arr['answer_best'] = 0;
            $answersData[] = $arr;
        }
        if($dataBest != null) {
            $dataBest['answer_best'] = 1;
            array_splice($answersData, 0, 0, [$dataBest,]);
        }
        $this->view->setVar('answers', $answersData);
    }
    
    /**
     * 问题预览
     * @date: 2016年1月28日 
     * @author: chenxiaolin
     */
    public function questionPreviewAction(){
        $this->tag->setTitle('问答详情');
        $id = $this -> request -> getQuery('id');
        if(empty($id)){
            echo "问题不存在！";
            $this -> view -> disable();
            return;
        }
        
        //问题内容
        $question = Question::query()
            ->columns([
                'u.user_id',
                'u.user_nickname',
                'u.user_cover',
                'u.user_gender',
                'Apps\Common\Models\Question.question_id as question_id',
                'Apps\Common\Models\Question.question_addtime as addtime',
                'Apps\Common\Models\Question.question_content as question_content',
                'Apps\Common\Models\Question.question_tag as question_tag',
                'Apps\Common\Models\Question.question_pics as question_pics',
                'Apps\Common\Models\Question.question_browsers as question_browsers',
                'Apps\Common\Models\Question.answer_num as answer_num',
                
            ])
            ->leftJoin('Apps\Common\Models\UserBase','Apps\Common\Models\Question.user_id=u.user_id','u')
            ->where('question_state>0 and question_id=:id:')
            ->bind(['id'=>$id])
            ->execute()->getFirst();
            
        if($question) {
            $question->question_pics = PicUrl::Question(json_decode($question->question_pics), $this->di);
            $question -> question_tag = json_decode($question -> question_tag);
            //$question->addtime = \Apps\Common\Libs\DateTime::FriendlyDate($question->addtime, 'mohu');
            $question->user_cover = PicUrl::UserCover($question->user_cover, $this->di);
            if (unserialize(base64_decode($question->question_content))){
                $question ->question_content = unserialize(base64_decode($question->question_content));
            }
        } else {
            echo '问题不存在！';
            $this->view->disable();
            return;
        }
            
        //美人儿已阅处头像    
        $covers = QuestionAnswer::query()
        ->columns([
            'distinct Apps\Common\Models\QuestionAnswer.user_id',
            'user.user_cover'
        ])
        -> leftJoin('Apps\Common\Models\UserBase','Apps\Common\Models\QuestionAnswer.user_id = user.user_id','user')
        ->where("Apps\Common\Models\QuestionAnswer.question_id = $id 
            and Apps\Common\Models\QuestionAnswer.answer_state = 1 
            and Apps\Common\Models\QuestionAnswer.type in (1,2)")
        ->order('answer_addtime desc')
        ->limit(7)
        ->execute() -> toArray();
        foreach ($covers as $k => $v){
            $covers[$k]['user_cover'] = PicUrl::UserCover($covers[$k]['user_cover'], $this -> getDI());
        }
        //专家回复
        $expert = QuestionAnswer::query()
            -> columns([
                'user.user_id',
                'user.user_nickname',
                'user.user_cover',
                'userAttr.attr_value_json',
                'Apps\Common\Models\QuestionAnswer.type',
                'Apps\Common\Models\QuestionAnswer.answer_content',
                'Apps\Common\Models\QuestionAnswer.answer_id',
                'Apps\Common\Models\QuestionAnswer.answer_addtime',
                'Apps\Common\Models\QuestionAnswer.support_count',
            ])
            -> leftJoin('Apps\Common\Models\UserBase','Apps\Common\Models\QuestionAnswer.user_id = user.user_id','user')
            -> leftJoin('Apps\Common\Models\UserAttribute','user.user_id = userAttr.user_id','userAttr')
            -> where("Apps\Common\Models\QuestionAnswer.question_id = $id 
                and Apps\Common\Models\QuestionAnswer.answer_state = 1 
                and Apps\Common\Models\QuestionAnswer.type = 2
                and userAttr.attr_type = 103 ")
            -> order('answer_addtime desc')
            -> execute() -> toArray();
        foreach ($expert as $k => $v){
            if (unserialize(base64_decode($expert[$k]['answer_content']))){
                $expert[$k]['answer_content'] = unserialize(base64_decode($expert[$k]['answer_content']));
            }
            if($expert[$k]['type']==2){
                $expert[$k]['reply']=QuestionAnswerComments::query()
                    ->columns([
                        'ub.user_nickname',
                        'ub.user_id',
                        'content',
                        'add_time',
                        'id',
                        'answer_id'
                    ])
                    ->leftJoin('Apps\Common\Models\UserBase', 'ub.user_id = Apps\Common\Models\QuestionAnswerComments.user_id', 'ub')
                    ->where("answer_id = ".$expert[$k]['answer_id'])
                    ->orderBy("add_time asc")
                    ->execute()->toArray();
                foreach($expert[$k]['reply'] as $key=>$v){
                    if(unserialize(base64_decode($expert[$k]['reply'][$key]['content']))){
                        $expert[$k]['reply'][$key]['content']=unserialize(base64_decode($expert[$k]['reply'][$key]['content']));
                    }
                }
            }
            $expert[$k]['user_cover'] = PicUrl::UserCover($expert[$k]['user_cover'], $this -> getDI());
            $expert[$k]['attr_value_json'] = json_decode($expert[$k]['attr_value_json'],true);
            $expert[$k]['attr_value_json']['field'] = implode('、', $expert[$k]['attr_value_json']['field']);
        }
        
        //网友回复
        $answer = QuestionAnswer::query()
        -> columns([
            'user.user_id',
            'user.user_nickname',
            'user.user_cover',
            'user.user_gender',
            'Apps\Common\Models\QuestionAnswer.answer_content',
            'Apps\Common\Models\QuestionAnswer.answer_pics',
            'Apps\Common\Models\QuestionAnswer.answer_addtime',
            'Apps\Common\Models\QuestionAnswer.original_content'
        ])
        -> leftJoin('Apps\Common\Models\UserBase','Apps\Common\Models\QuestionAnswer.user_id = user.user_id','user')
        -> where("Apps\Common\Models\QuestionAnswer.question_id = $id
            and Apps\Common\Models\QuestionAnswer.answer_state = 1
            and Apps\Common\Models\QuestionAnswer.type = 1")
            -> order('answer_addtime desc')
            -> execute() -> toArray();
        foreach ($answer as $k => $v){
            if (unserialize(base64_decode($answer[$k]['answer_content']))){
                $answer[$k]['answer_content'] = unserialize(base64_decode($answer[$k]['answer_content']));
            }
            if (unserialize(base64_decode($answer[$k]['original_content']))){
                $answer[$k]['original_content'] = unserialize(base64_decode($answer[$k]['original_content']));
            }
            $answer[$k]['user_cover'] = PicUrl::UserCover($answer[$k]['user_cover'], $this -> getDI());
            $answer[$k]['answer_pics'] = PicUrl::Question(json_decode($answer[$k]['answer_pics']), $this->di);
        }
        $this->view->setVar('question', $question);
        $this->view->setVar('covers', $covers);
        $this -> view -> setVar('expert', $expert);
        $this -> view -> setVar('answer', $answer);
    }

    /**
     * 活动
     */
    public function activityAction()
    {
        $id = $this->request->getQuery('id');
        if(empty($id)) {
            echo '活动不存在！';
            $this->view->disable();
            return;
        }

        $activity = Activity::query()
            ->columns(['activity_id id',
                'city_id city',
                'activity_type type',
                'activity_title as title',
                'activity_intro as intro',
                '0 beauty_parlors',
            ])
            ->where('activity_id=:id:')
            ->bind(['id'=>$id])
            ->execute()->getFirst();
        if(!$activity) {
            echo '活动不存在！';
            $this->view->disable();
            return;
        }

        $this->view->setVar('activity', $activity);
        $this->tag->setTitle( $activity->title );

        $parlors = ActivityBeautyParlor::query()
            ->columns([
                'bp_id as id',
                'bp_name as name',
                'bp_cover as cover',
                'bp_address as address',
                '0 as tags',
                'bp_medal as medal'])
            ->leftJoin('Apps\Common\Models\BeautyParlor',
                'Apps\Common\Models\ActivityBeautyParlor.beauty_parlor_id=bp.bp_id','bp')
            ->where('bp.bp_state > 0 and activity_id = :id:')
            ->bind(['id' => $id])
            ->execute()->toArray();

        for($i = 0; $i < count($parlors); $i++) {
            $parlors[$i]['cover'] = PicUrl::BeautyParlorCover($parlors[$i]['cover'], $this->getDi());

            $medal = $parlors[$i]['medal'];
            unset($parlors[$i]['medal']);
            $parlors[$i]['medal_refund'] = \Apps\Common\Libs\BeautyParlor::getMedalRefund($medal);
            $parlors[$i]['medal_verify'] = \Apps\Common\Libs\BeautyParlor::getMedalVerify($medal);
            $parlors[$i]['tags'] = \Apps\Common\Libs\BeautyParlor::getTags($parlors[$i]['id']);
        }
        $this->view->setVar('parlors', $parlors);
    }
    
    /**
     * 活动预览
     * @date: 2016年1月12日 
     * @author: chenxiaolin
     */
    public function activityPreviewAction(){
        $id = $this->request->getQuery('id');
        if(empty($id)) {
            echo '活动不存在！';
            $this->view->disable();
            return;
        }
        //活动内容
        $date = date('Y-m-d H:i:s');
        $activity = Activity::query()
            ->columns([
                'Apps\Common\Models\Activity.activity_id',
                'activity_title',
                'activity_cover',
                'activity_intro',
                'out_link',
                'activity_addtime',
                'views',
                'comments',
            ])
            ->where("activity_id = $id and activity_state in (0,1)")
            ->execute()->getFirst();
            
            if(!$activity) {
                echo '活动不存在！';
                $this->view->disable();
                return;
            }else{
                $activity = $activity -> toArray();
            }
            
        $activity['activity_cover'] = PicUrl::ActivityCover($activity['activity_cover'], $this -> getDI());
        //活动回复
        $comment = ActivityComment::query()
            ->columns([
                'comment_content',
                'user.user_nickname',
                'user.user_cover',
                'user.user_gender'
            ])
            -> leftJoin('Apps\Common\Models\UserBase','Apps\Common\Models\ActivityComment.comment_user_id = user.user_id','user')
            ->where("Apps\Common\Models\ActivityComment.activity_id = $id and Apps\Common\Models\ActivityComment.state = 1")
            ->order('addtime desc')
            ->execute() -> toArray();
        foreach ($comment as $k => $v){
            $comment[$k]['user_cover'] = PicUrl::UserCover($comment[$k]['user_cover'], $this -> getDI());
        }
        $lastTime = ActivityComment::maximum(array("activity_id = $id and state = 1",'column' => 'addtime'));
        
        //美人儿已阅处显示的头像
        $covers = ActivityComment::query()
        ->columns([
            'distinct comment_user_id',
            'user.user_cover'
        ])
        -> leftJoin('Apps\Common\Models\UserBase','Apps\Common\Models\ActivityComment.comment_user_id = user.user_id','user')
        ->where("Apps\Common\Models\ActivityComment.activity_id = $id and Apps\Common\Models\ActivityComment.state = 1")
        ->order('addtime desc')
        ->limit(7)
        ->execute() -> toArray();
        foreach ($covers as $k => $v){
            $covers[$k]['user_cover'] = PicUrl::UserCover($covers[$k]['user_cover'], $this -> getDI());
        }
        

        $this->view->setVar('activity', $activity);
        $this -> view -> setVar('comment', $comment);
        $this -> view -> setVar('lastTime', $lastTime);
        $this -> view -> setVar('covers', $covers);
        $this->tag->setTitle( $activity['activity_title'] );
    }

    /**
     * 美容院
     */
    public function beautyParlorAction()
    {
        $id = $this->request->getQuery('id');
        if(empty($id)) {
            echo '美容院不存在！';
            $this->view->disable();
            return;
        }
        /**
         * 查询基础信息
         */
        $info = BeautyParlor::query()
            ->columns([
                'bp_id as id',
                'bp_name as name',
                'bp_cover as cover',
                'bp_address as address',
                'bp_tel as tel',
                'bp_medal as medal',
                'bp_intro as intro',
                'X(bp_coordinate) latitude',
                'Y(bp_coordinate) longitude',
            ])
            ->where('bp_id=:id:')
            ->bind(['id' => $id])
            ->execute()->getFirst();

        if(!$info) {
            echo '美容院不存在！';
            $this->view->disable();
            return;
        }

        $parlor = [];
        $parlor['id'] = $info->id;
        $parlor['name'] = $info->name;
        $parlor['cover'] = $info->cover;
        $parlor['address'] = $info->address;
        $parlor['tel'] = $info->tel;
        $parlor['intro'] = $info->intro;
        $parlor['latitude'] = $info->latitude;
        $parlor['longitude'] = $info->longitude;
        $parlor['medal_refund'] = \Apps\Common\Libs\BeautyParlor::getMedalRefund($info->medal);
        $parlor['medal_verify'] = \Apps\Common\Libs\BeautyParlor::getMedalVerify($info->medal);

        /**
         * 增加图片列表
         */
        $pics = BeautyParlorPhotos::query()
            ->columns(['photo_url url'])
            ->where('photo_state>0 and beauty_parlor_id=:bid:')
            ->bind(['bid'=>$id])
            ->execute()->toArray();
        $parlor['pics'] = [];
        foreach($pics as $pic) {
            $parlor['pics'][] = PicUrl::BeautyParlorPic($pic['url'], $this->getDi());
        }
        /**
         * 增加服务范围列表
         */
        $service = BeautyParlorService::query()
            ->columns([
                'service_id id',
                'service_cover cover',
                'service_name name',
                'service_price price',
                'service_unit unit',
                'service_description description'])
            ->where('service_state>0 and beauty_parlor_id=:bid:')
            ->bind(['bid'=>$id])
            ->execute()->toArray();
        $parlor['services'] = [];
        foreach($service as $d) {
            $d['cover'] = PicUrl::BeautyParlorCover($d['cover'], $this->getDi());
            $unit = $d['unit'];
            unset($d['unit']);
            $d['price_show'] = '';
            if($d['price'] > 0) {
                $d['price_show'] = $d['price'] . '元' . ($unit ? '/' . $unit : '');
            }
            $parlor['services'][] = $d;
        }
        // 评论总数
        $parlor['comment_num'] = BeautyParlorComment::count(
            [
                'conditions' => 'beauty_parlor_id = :bid:',
                'bind' => [
                    'bid' => $id,
                ]
            ]
        );
        $this->view->setVar('parlor', $parlor);
        $this->tag->setTitle($parlor['name']);
        $this->assets->collection('footer')
            ->addJs('/libs/zepto/zepto-1.1.6.min.js', false)
            ->addJs('/libs/iscroll/iscroll.js', false)
            ->addJs('/assets/frontend/js/share/bp-view.js', false);
    }
    
    /**
     * 预览美容院
     * @date: 2016年1月28日 
     * @author: chenxiaolin
     */
    public function beautyParlorPreviewAction(){
        $id = $this->request->getQuery('id');
        if(empty($id)) {
            echo '美容院不存在！';
            $this->view->disable();
            return;
        }
        //美容院
        $parlor = BeautyParlor::query()
            -> columns([
                'Apps\Common\Models\BeautyParlor.bp_id',
                'Apps\Common\Models\BeautyParlor.bp_name as name',
                'Apps\Common\Models\BeautyParlor.bp_address',
                'Apps\Common\Models\BeautyParlor.bp_medal',
                'Apps\Common\Models\BeautyParlor.bp_intro',
                'bpAttr.low_price',
                'bpAttr.open_time',
                'bpAttr.close_time',
                'bpAttr.open_year',
                'bpAttr.shop_area',
                'bpAttr.service_num',
                'bpAttr.facilities',
                
            ])
            -> leftJoin('Apps\Common\Models\BeautyParlorAttr','Apps\Common\Models\BeautyParlor.bp_id = bpAttr.beauty_parlor_id','bpAttr')
            -> where("Apps\Common\Models\BeautyParlor.bp_id = $id")
            -> execute() -> getFirst();
        if($parlor){
            $parlor -> bp_medal = \Apps\Common\Libs\BeautyParlor::getMedalVerify($parlor -> bp_medal);
            $parlor -> facilities = json_decode($parlor -> facilities,true);
        }else{
            echo '美容院不存在！';
            $this->view->disable();
            return;
        }
        //标签
        $tag = BeautyParlorTagInfo::query()
            -> columns(['tag_title'])
            -> leftJoin('Apps\Common\Models\BeautyParlorTag','bpTag.tag_id = Apps\Common\Models\BeautyParlorTagInfo.tag_id','bpTag')
            -> where("bpTag.bp_id = $id and Apps\Common\Models\BeautyParlorTagInfo.tag_state = 1")
            -> execute() -> toArray();
        
        //轮播图片
        $photos = BeautyParlorPhotos::query()
            -> columns([
                'photo_url'
            ])
            -> where("beauty_parlor_id = $id and photo_state = 1")
            -> execute() -> toArray();
        foreach ($photos as $k => $v){
            $photos[$k]['photo_url'] = PicUrl::BeautyParlorPic($photos[$k]['photo_url'], $this -> getDI());
        }
        //服务项目
        $service = BeautyParlorService::query()
            -> columns([
                'service_name',
                'service_price',
                'service_cover'
            ])
            -> where("beauty_parlor_id = $id and service_state = 1")
            -> execute() -> toArray();
        foreach ($service as $k => $v){
            $service[$k]['service_cover'] = PicUrl::BeautyParlorCover($service[$k]['service_cover'], $this -> getDI());
        }
        //美粒互动发布人
        $interact = BeautyParlorInteractive::query()
            ->where("bp_id = $id and state = 1")
            ->orderBy('addtime desc')
            ->execute()
            ->getFirst();
        if(!empty($interact)){
            $interactUser = UserBase::findFirst("user_id = $interact->user_id");
            $interactUser->user_cover = PicUrl::UserCover($interactUser->user_cover, $this->getDI());
            if (unserialize(base64_decode($interactUser->persional_sign))){
                $interactUser->persional_sign = unserialize(base64_decode($interactUser->persional_sign));
            }
        }
        //美粒互动
        $interactList = BeautyParlorInteractive::query()
        ->columns([
            'content',
            'pics',
            'addtime',
            'state as count',
            'id'
        ])
        ->where("bp_id = $id and state = 1")
        ->order('addtime desc,id desc')
        ->limit(3)
        ->execute()
        ->toArray();
        foreach ($interactList as $k => $v){
            $str = json_decode($v['pics'],true);
            if(!empty($str[0])){
                $interactList[$k]['pics'] = PicUrl::InteractivePics($str[0], $this->getDI());
            }else{
                $interactList[$k]['pics'] = null;
            }
            $interactList[$k]['count'] = count($str);
        }
        //用户评论
        $comment = BeautyParlorComment::query()
            -> columns([
                'u.user_id',
                'u.user_nickname',
                'u.user_cover',
                'comment_addtime',
                'comment_info',
                'comment_photos',
                'comment_id'
            ])
            ->leftJoin('Apps\Common\Models\UserBase','Apps\Common\Models\BeautyParlorComment.user_id=u.user_id','u')
            -> where("Apps\Common\Models\BeautyParlorComment.beauty_parlor_id = $id and Apps\Common\Models\BeautyParlorComment.comment_state = 1")
            ->orderBy('comment_addtime desc,comment_id desc')
            ->limit(3)
            -> execute() -> toArray();
        foreach ($comment as $k => $v){
            $comment[$k]['user_cover'] = PicUrl::UserCover($comment[$k]['user_cover'], $this -> getDI());
            $comment[$k]['comment_photos'] = PicUrl::BeautyParlorComment(json_decode($comment[$k]['comment_photos'],true), $this -> getDI());
            
            if (unserialize(base64_decode($comment[$k]['comment_info']))){
                $comment[$k]['comment_info'] = unserialize(base64_decode($comment[$k]['comment_info']));
            }
        }
        $this -> view -> setVar('parlor', $parlor);
        $this -> view -> setVar('photos', $photos);
        $this -> view -> setVar('service', $service);
        $this -> view -> setVar('tag', $tag);
        $this -> view -> setVar('comment', $comment);
        $this -> view -> setVar('interactUser', $interactUser);
        $this -> view -> setVar('interactList', $interactList);
        $this->tag->setTitle($parlor['name']);
        $this->assets->collection('footer')
        ->addJs('/libs/zepto/zepto-1.1.6.min.js', false)
        ->addJs('/libs/iscroll/iscroll.js', false)
        ->addJs('/assets/frontend/js/share/bp-view.js', false);
    }

    /**
     * 美容院服务
     */
    public function beautyParlorServiceAction()
    {
        $id = $this->request->getQuery('id');
        if(empty($id)) {
            echo '美容院服务项目不存在！';
            $this->view->disable();
            return;
        }
        $service = BeautyParlorService::query()
            ->columns([
                'service_id as id',
                'service_cover cover',
                'service_name as name',
                'service_price as price',
                'service_unit as price_show',
                'service_description as description',
                'service_intro as intro',
                'beauty_parlor_id as bp_id'
            ])
            ->where('service_state>0 and service_id=:id:')
            ->bind(['id' => $id])
            ->execute()->getFirst();
        if(!$service) {
            echo '美容院服务项目不存在！';
            $this->view->disable();
            return;
        }
        $data = [
            'id' => $service->id,
            'cover' => PicUrl::BeautyParlorCover($service->cover, $this->getDi()),
            'name' => $service->name,
            'price' => $service->price,
            'description' => $service->description,
            'intro' => \Apps\Common\Libs\BeautyParlor::formatServiceIntro($service->intro, $this->di),
        ];
        if($service->price > 0) {
            $unit = $service->price_show;
            $data['price_show'] = $service->price . '元' . ($unit ? '/' . $unit : '');
        }
        $data['beauty_parlor'] = BeautyParlor::query()
            ->columns([
                'bp_id as id',
                'bp_name as name',
                'bp_medal as medal',
            ])
            ->where('bp_id=:id:')
            ->bind(['id' => $service->bp_id])
            ->execute()->getFirst();

        $this->view->setVar('service', $data);
        $this->tag->setTitle($data['beauty_parlor']->name);
    }
    
    /**
     * 服务项目预览
     * @date: 2016年1月29日 
     * @author: chenxiaolin
     */
    public function servicePreviewAction(){
        $id = $this->request->getQuery('id');
        if(empty($id)) {
            echo '美容院服务项目不存在！';
            $this->view->disable();
            return;
        }
        
        $service = BeautyParlorService::query()
        ->columns([
            'service_id as id',
            'service_cover cover',
            'service_name as name',
            'service_price as price',
            'service_unit as unit',
            'service_intro as intro',
            'beauty_parlor_id as bp_id'
        ])
        ->where('service_state>0 and service_id=:id:')
        ->bind(['id' => $id])
        ->execute()->getFirst();
        
        if(!$service) {
            echo '美容院服务项目不存在！';
            $this->view->disable();
            return;
        }
        
        $service -> cover = PicUrl::BeautyParlorCover($service -> cover, $this -> getDI());        
        
        $bp_info = BeautyParlor::query()
        ->columns([
            'bp_id',
            'bp_name',
            'bp_address',
            'bp_owner_mobile',
        ])
        ->where('bp_id=:id:')
        ->bind(['id' => $service->bp_id])
        ->execute()->getFirst();
        
        $this->view->setVar('service', $service);
        $this->view->setVar('bp', $bp_info);
        $this->tag->setTitle($bp_info -> bp_name);
    }
    
    /**
     * 美粒宝典预览
     * @date: 2016年1月4日 
     * @author: chenxiaolin
     */
   
    public function beautyCanonAction(){
        $id = $this->request->getQuery('id');
        if(empty($id)) {
            echo '美粒宝典不存在！';
            $this->view->disable();
            return;
        }
        $canon = CosBeautyCanon::query()
            ->columns([
                'title',
                'content'
            ])
            ->where('id=:id:')
            ->bind(['id' => $id])
            ->execute()->getFirst();
        if(!$canon) {
            echo '美粒宝典不存在！';
            $this->view->disable();
            return;
        }
        
        $this->view->setVar('canon', $canon);
        $this->tag->setTitle($canon->title);
    }




}